package com.education.service.system;

import com.education.common.exception.BusinessException;
import com.education.common.utils.ObjectUtils;
import com.education.common.utils.Result;
import com.education.common.utils.ResultCode;
import com.education.mapper.system.SystemRoleMapper;
import com.education.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 余一
 * @Description
 * @date 2021/7/10 16:32
 */
@Service
public class SystemRoleService extends BaseService<SystemRoleMapper> {
    @Autowired
    private SystemRoleMenuService systemRoleMenuService;
    @Autowired
    private SystemAdminRoleService systemAdminRoleService;
    @Override
    public Result saveOrUpdate(Map roleMap) {
        synchronized (this){
            //检查该角色是否存在
            Map rMap = mapper.findRoleByName((String) roleMap.get("name"));
            if (ObjectUtils.isNotEmpty(rMap)) {
                return Result.fail(ResultCode.FAIL,"角色已存在，请勿重复添加");
            }
        }
        return super.saveOrUpdate(roleMap);
    }

    public Result queryList(Map params){
        return Result.success(mapper.queryList(params));
    }

    @Override
    @Transactional
    public Result deleteById(Integer id) {
        try {
            super.deleteById(id);
            //删除角色关联的权限
            systemRoleMenuService.deleteById(id);
            //删除用户关联的角色
            systemAdminRoleService.deleteById(id);
            return Result.success(ResultCode.SUCCESS,"删除成功");
        }catch (Exception e){
            logger.error("删除角色异常",e);
            throw new BusinessException(new ResultCode(ResultCode.FAIL,"删除角色异常"));
        }
    }

    @Transactional
    public  Result batchDeleteByIds(List<Integer> ids){
        try {
            mapper.batchDeleteByIds(ids);
            //删除角色关联的权限
            systemRoleMenuService.batchDeleteByIds(ids);
            //删除用户关联的角色
            systemAdminRoleService.batchDeleteByIds(ids);
            return Result.success(ResultCode.SUCCESS,"删除成功");
        }catch (Exception e){
            logger.error("删除角色异常",e);
            throw new BusinessException(new ResultCode(ResultCode.FAIL,"删除角色异常"));
        }
    }

    @Transactional
    public Result savePermission(Map params) {
        try{
            Integer roleId = (Integer) params.get("roleId");
            //先删除原先的角色权限
            systemRoleMenuService.deleteByRoleId(roleId);
            List<Integer> permissions = (List<Integer>) params.get("permission");
            if (ObjectUtils.isNotEmpty(permissions)) {
                List<Map> roleMenus = new ArrayList<>();
                permissions.forEach(menuId -> {
                    Map roleMenu = new HashMap();
                    roleMenu.put("role_id",roleId);
                    roleMenu.put("menu_id",menuId);
                    roleMenus.add(roleMenu);
                });
                systemRoleMenuService.batchSave(roleMenus);
            }
            return Result.success(ResultCode.SUCCESS,"添加成功");
        }catch (Exception e){
            logger.error("保存角色权限异常");
            throw new BusinessException(new ResultCode(ResultCode.FAIL,"保存角色权限异常"));
        }
    }
}
